package etnorservice.etnor_service.controller;

import etnorservice.etnor_service.domain.po.BaseArticle;
import etnorservice.etnor_service.domain.po.DocumentVO;
import etnorservice.etnor_service.domain.po.User;
import etnorservice.etnor_service.domain.po.UserDetails;
import etnorservice.etnor_service.domain.vo.ArticleVO;
import etnorservice.etnor_service.request.ArticleRequest;
import etnorservice.etnor_service.request.BlogCreateRequest;
import etnorservice.etnor_service.result.Result;
import etnorservice.etnor_service.service.ArticleService;
import etnorservice.etnor_service.service.UserService;
import etnorservice.etnor_service.utils.AesUtil;
import etnorservice.etnor_service.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.service.ResponseMessage;

import java.io.File;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: cc
 * @Date: 2024/10/14/18:02
 **/
@RestController
public class ArticleController {
    @Autowired
    ArticleService articleService;
    @Autowired
    UserService userService;
    @GetMapping("/system/doc/article/api-docs")
    public Result getArticleList(){
        Result result=new Result();
        result.setData(articleService.articleList());
        return result;
    }
    @PostMapping("/system/doc/article/details")
    public Result getArticleById(@RequestBody ArticleRequest articleRequest){
        Result result=new Result();
        result.setData(articleService.findArticleById(articleRequest.getArticleId(), articleRequest.getLanguage()));
        return result;
    }
    //公告文章
    @PostMapping("/doc/article/details")
    public Result getArticle(@RequestBody ArticleRequest articleRequest){
        Result result=new Result();

        ArticleVO articleById = articleService.findArticleById(articleRequest.getArticleId(), articleRequest.getLanguage());
        System.out.println(articleById);
        if(articleById.getState().equals("0")){
            result.setCode(20001);
            result.setMessage("原文已经下架......");
        }
        if(articleById.getState().equals("1")) {
            result.setCode(200);
            result.setData(articleById);
        }
        return result;
    }
    @PostMapping("/system/editDoc/save")
    public Result SaveEditDoc(@RequestBody ArticleRequest articleRequest
            ) throws IOException {

        articleService.saveArticle(articleRequest.getArticleId(), articleRequest.getContent(), articleRequest.getLanguage());
        return new Result();
    }

    @GetMapping("/system/doc/{authorId}")
    public Result getArticleListByAccount(
            @PathVariable("authorId") String authorId,
            @RequestParam("spaceId") String spaceId) throws Exception {
        Result result=new Result();
        spaceId= AesUtil.decrypt(spaceId);
        authorId=AesUtil.decrypt(authorId);
        result.setData(articleService.findArticleByIdAndAccount(authorId,spaceId));
        return result;
    }
    @GetMapping("/doc/base/request")
    public Result findBaseParams(@RequestParam("account") String accountId){
        Result result=new Result();
        result.setData(articleService.findBaseParams(accountId));
        return result;
    }

    @PostMapping("/doc/saveBlogs")
    public Result saveBlogs(@RequestBody BlogCreateRequest blogCreateRequest){
        articleService.addBlogs(blogCreateRequest);

        return null;
    }


    @GetMapping("/doc/getArticle")
    public Result getArticle(
            @RequestParam("lang") String language,
            @RequestParam("articleId") String articleId,
            @RequestHeader(value = "Authorization", required = false)
                    String authorizationHeader
    ){
        String account=
                new JwtTokenUtil().
                        getAccountFromToken(extractTokenFromHeader(authorizationHeader));
        UserDetails user = userService.findUserDetail(account);

        DocumentVO documentVO= articleService.getArticle(articleId,language);
        Integer visibality=documentVO.getVisibility();
        Result result = new Result();
        if(documentVO.getBaseArticle()==null){
            result.setMessage("文章不存在!");
            result.setCode(200404);
        }else {
            if (visibality == 0) {
                if (user!=null&&documentVO.getBaseArticle().getAuthorId().equals(user.getAccount())) {
                    result.setData(documentVO);
                } else {
                    result.setCode(200401);
                    result.setMessage("文章为私密状态，仅允许自己查看");
                }
            }
            if (visibality == 1) {
                if (user != null) {
                    result.setData(documentVO);
                } else {
                    result.setMessage("文章属于内部资料，需登录查看");
                    result.setCode(200402);
                }
            }
            if (visibality == 2) {
                result.setData(documentVO);
                result.setCode(200200);
            }
        }


   return result;

    }




    private String extractTokenFromHeader(String header) {
        if (header == null || !header.startsWith("Bearer ")) {
            return null;
        }
        return header.substring(7).trim(); // 去除 "Bearer ".length() 后面的空格
    }
}
